<?php echo html::style('media/js/datatable/media/css/jquery.dataTables.css'); ?>
<?php echo html::style('media/js/datatable/media/css/demo_page.css'); ?>
<?php echo html::style('media/js/datatable/media/css/demo_table.css'); ?>
<?php echo html::style('media/js/datatable/media/css/jquery.dataTables_themeroller.css'); ?>
<style type="text/css">
    td{
        color: black;
    }
</style>
<?php echo html::script('media/js/datatable/media/js/jquery.dataTables.min.js') ?>
<?php echo html::script('media/js/functions.js') ?>
<?php echo form::open() ?>
<?php echo form::label('search', 'Keresés: ') ?>
<?php echo form::input('search') ?>
<select name="search_category">
    <option value="media">cím</option>
    <option value="author">szerző</option>
    <option value="world">világ</option>
    <option value="series">sorozat</option>
    <option value="isbn">ISBN</option>
</select>
<?php echo form::submit('submit', 'Keresés') ?>
<?php echo form::close() ?>

<?php //if(isset($result)) var_dump($result) ?>

<br/>
<?php if (isset($result)) : ?>
    <?php if ($result_type != 'author') : ?>
        <div id="demo">
            Találatok:<br/>
            <table class="display" id="example" width="100%">
                <thead>
                    <tr>
                        <th>Cím</th>
                        <th>Megjelenés éve</th>
                        <th>Szerző(k)</th>
                        <th>Világ</th>
                        <?php if ($result_type == 'isbn') : ?>
                            <th>ISBN</th>
                        <?php endif; ?>
                        <?php if ($result_type == 'series') : ?>
                            <th>Sorozat</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($result as $res) : ?>
                        <tr>
                            <td class="center"><a href="<?php echo Url::site('books/showmedia/' . $res->id, TRUE, FALSE) ?>"><?php echo $res->title ?></a></td>
                            <td class="center"><?php echo $res->release_date ?></td>
                            <td class="center">
                                <?php $author_ids = ORM::factory('authormedia')->where('media_id', '=', $res->id)->find_all() ?>
                                <?php
                                foreach ($author_ids as $a) {
                                    $author = ORM::factory('author')->where('id', '=', $a->author_id)->find();
                                    if ($author->second_name_first == 1) {
                                        echo '<a href='.Url::site('books/showauthor/'.$a->id, TRUE, FALSE).'>'.$author->second_name . ' ' . $author->first_name . '</a><br />';
                                    } else {
                                        echo '<a href='.Url::site('books/showauthor/'.$a->id, TRUE, FALSE).'>'.$author->first_name . ' ' . $author->second_name . '</a><br />';
                                    }
                                }
                                ?>
                            </td>
                            <td class="center">
                                <?php $world = ORM::factory('world')->where('id', '=', $res->world_id)->find(); ?>
                                <a href="<?php echo Url::site('books/showworld/' . $res->world_id, TRUE, FALSE) ?>"><?php echo $world->name ?></a>
                            </td>
                            <?php if ($result_type == 'isbn') : ?>
                                <td class="center">
                                    <?php echo $res->ISBN ?>
                                </td>
                            <?php endif ?>
                            <?php if ($result_type == 'series') : ?>
                                <td class="center">
                                    <?php echo $res->series ?>
                                </td>
                            <?php endif ?>
                        </tr>
                    <?php endforeach ?>
                </tbody>
            </table>
        </div>
    <?php else : ?>
        <div id="demo">
            Találatok:<br/>
            <table class="display" id="example" width="100%">
                <thead>
                    <tr>
                        <th>Cím</th>
                        <th>Megjelenés éve</th>
                        <th>Szerző(k)</th>
                        <th>Világ</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($result as $res): ?>
                        <?php $media_ids = ORM::factory('authormedia')->where('author_id', '=', $res->id)->find_all() ?>
                        <?php foreach ($media_ids as $id) : ?>
                            <?php $media = ORM::factory('books')->where('id', '=', $id)->find() ?>
                            <tr>
                                <td class="center"><a href="<?php echo Url::site('books/showmedia/' . $media->id, TRUE, FALSE) ?>"><?php echo $media->title ?></a></td>
                                <td class="center"><?php echo $media->release_date ?></td>
                                <td class="center">
                                    <?php
                                    if ($res->second_name_first == 1) {
                                        echo '<a href='.Url::site('books/showauthor/'.$res->id, TRUE, FALSE).'>'.$res->second_name . ' ' . $res->first_name . '</a><br />';
                                    } else {
                                        echo '<a href='.Url::site('books/showauthor/'.$res->id, TRUE, FALSE).'>'.$res->first_name . ' ' . $res->second_name . '</a><br />';
                                    }
                                    ?>
                                </td>
                                <td class="center">
                                    <?php $world = ORM::factory('world')->where('id', '=', $media->world_id)->find(); ?>
                                    <a href="<?php echo Url::site('books/showworld/' . $media->world_id, TRUE, FALSE) ?>"><?php echo $world->name ?></a>
                                </td>
                            </tr>
                        <?php endforeach ?>
                    <?php endforeach ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
<?php endif; ?>